using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Markup;
using jSLCore.Client.Utility;

namespace jSLCore.Client.Presentation.Controls
{
    /// <summary>
    /// Interaction logic for LeftHeaderContainer.xaml
    /// </summary>
    [ContentProperty("ValueContent")]
    public partial class LeftHeaderContainer : UserControl
    {
        #region DependencyProperty
        public static readonly DependencyProperty HeaderProperty =
            DependencyProperty.Register("HeaderProperty", typeof(object), typeof(LeftHeaderContainer),
                                        new PropertyMetadata(LeftContent_Changed));



        public static readonly DependencyProperty ValueContentProperty =
            DependencyProperty.Register("ValueContentProperty", typeof(object), typeof(LeftHeaderContainer),
                                        new PropertyMetadata(RightContent_Changed));

        private static void RightContent_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var headerContainer = d as LeftHeaderContainer;
            if (headerContainer != null) headerContainer.tightContent.Content = e.NewValue;
        }

        private static void LeftContent_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var headerContainer = d as LeftHeaderContainer;
            if (headerContainer != null) headerContainer.leftContent.Content = e.NewValue;
        }

        public object Header
        {
            get
            {
                return GetValue(HeaderProperty);
            }
            set
            {
                SetValue(HeaderProperty, value);
            }
        }

        public object ValueContent
        {
            get
            {
                return GetValue(ValueContentProperty);
            }
            set
            {
                SetValue(ValueContentProperty, value);
            }
        }

        #endregion


        private static ObservableCollection<LeftHeaderContainer> controls= new ObservableCollection<LeftHeaderContainer>();
        public LeftHeaderContainer()
        {
            ShareGroupName = null;
            //InitializeComponent();
            IsTabStop = false;
            var grid = new Grid();
            leftColumn = new ColumnDefinition {Width = GridLength.Auto};
            splitterColumn = new ColumnDefinition { Width = new GridLength(10) };
            rightColumn = new ColumnDefinition() ;
            grid.ColumnDefinitions.Add(leftColumn);
            grid.ColumnDefinitions.Add(splitterColumn);
            grid.ColumnDefinitions.Add(rightColumn);
            leftContent = new ContentControl {IsTabStop=false ,VerticalAlignment = VerticalAlignment.Center, HorizontalContentAlignment = HorizontalAlignment.Stretch};
            tightContent = new ContentControl {IsTabStop=false, VerticalAlignment = VerticalAlignment.Center,HorizontalContentAlignment = HorizontalAlignment.Stretch};



            Loaded += (sender, args) =>
                               {
                                   controls.Add(this);
                                   BaseControl = this.FindAncestor<BaseControl.BaseControl>();

                                   leftContent.LayoutUpdated += (o, eventArgs) => ResetLeftSize();
                               };
            


            grid.Children.Add(leftContent);
            grid.Children.Add(tightContent);

            Grid.SetColumn(leftContent, 0);
            Grid.SetColumn(tightContent, 2);

            Content = grid;

            IsContentFillGrid = false;
            IsContentFillGrid = true;

            Unloaded+=OnUnloaded;
            
        }

        private void OnUnloaded(object sender, RoutedEventArgs routedEventArgs)
        {
            controls.Remove(this);
        }

        public VerticalAlignment LeftAlignment
        {
            get { return leftContent.VerticalAlignment; }
            set { leftContent.VerticalAlignment = value; }
        }

        public VerticalAlignment RightAlignment
        {
            get { return tightContent.VerticalAlignment; }
            set { tightContent.VerticalAlignment = value; }
        }

        public string ShareGroupName { get; set; }

        private void ResetLeftSize()
        {
            if (controls.Count > 0)
            {

                var @group = controls.Where(c => c.isWindowShared == isWindowShared &&
                                                 ShareGroupName == c.ShareGroupName &&
                                                 c.Parent == Parent);

                if (@group.Any())
                {
                    var max = @group.Max(c => c.leftContent.ActualWidth);
                    foreach (var control in @group)
                    {
                        if (max != 0)
                            if (control != this) control.leftContent.Width = max;
                    }
                }
            
            }
        }

        internal BaseControl.BaseControl BaseControl;

        public bool IsContentFillGrid
        {
            get
            {
                return tightContent.HorizontalAlignment == HorizontalAlignment.Stretch;
            }
            set
            {
                tightContent.HorizontalAlignment = value ? HorizontalAlignment.Stretch : HorizontalAlignment.Left;
                
            }
        }

        private ColumnDefinition leftColumn;
        private ColumnDefinition splitterColumn;
        private ColumnDefinition rightColumn;
        public ContentControl leftContent;
        public ContentControl tightContent;
        private bool isWindowShared = false;
        public bool IsWindowShared
        {
            get { return isWindowShared; }
            set
            {
                isWindowShared = value;

            }
        }

        #region Left Column
        public double LeftMaxWidth
        {
            get { return leftColumn.MaxWidth; }
            set { leftColumn.MaxWidth = value; }
        }

        public double LeftMinWidth
        {
            get { return leftColumn.MinWidth; }
            set { leftColumn.MinWidth = value; }
        }

        public GridLength LeftWidth
        {
            get { return leftColumn.Width; }
            set { leftColumn.Width = value; }
        }

        public double LeftActualWidth
        {
            get { return leftColumn.ActualWidth; }
        }
        public object LeftContent
        {
            get { return leftContent.Content; }
            set { leftContent.Content = value; }
        }

        #endregion

        #region Splitter
        public GridLength SplitterWidth
        {
            get { return splitterColumn.Width; }
            set { splitterColumn.Width = value; }
        }
        #endregion

        #region Right column
        public double RightActualWidth
        {
            get { return rightColumn.ActualWidth; }
        }

        public double RightMaxWidth
        {
            get { return rightColumn.MaxWidth; }
            set { rightColumn.MaxWidth = value; }
        }

        public double RightMinWidth
        {
            get { return rightColumn.MinWidth; }
            set { rightColumn.MinWidth = value; }
        }

        public GridLength RightWidth
        {
            get { return rightColumn.Width; }
            set { rightColumn.Width = value; }
        }


        public object RightContent
        {
            get { return tightContent.Content; }
            set { tightContent.Content = value; }
        }
        #endregion



    }
}